package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.util.StringUtil2;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import net.zigin.nhi.could.waste.constants.CommonConstants;
import net.zigin.nhi.could.waste.dto.SysUserStaffDto;
import net.zigin.nhi.could.waste.exception.EpiCloudException;
import net.zigin.nhi.could.waste.model.SysUser;
import net.zigin.nhi.could.waste.service.SysUserService;
import net.zigin.nhi.could.waste.util.CodeUtil;
import net.zigin.nhi.could.waste.util.EncryptDataUtil;
import net.zigin.nhi.could.waste.vo.SysUserStaffVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.dao.BaseMapper;
import net.zigin.nhi.could.waste.dao.SysUserStaffMapper;
import net.zigin.nhi.could.waste.model.SysUserStaff;
import net.zigin.nhi.could.waste.service.SysUserStaffService;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-7 19:08:59                                               
##################################################################*/


/**
 * SysUserStaff服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: SysUserStaffServiceImpl
 * @date 2021-9-7 19:08:59
 */
@Service
public class SysUserStaffServiceImpl extends AbstractServiceImpl<SysUserStaff, String> implements SysUserStaffService {
    @Autowired
    private SysUserStaffMapper sysUserStaffMapper;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public BaseMapper<SysUserStaff, String> getMapper() {
        return sysUserStaffMapper;
    }

    @Override
    public String saveOrUpdate(SysUserStaffDto userStaffDto) {
        AssertUtil.isNull(userStaffDto, "员工信息不能为空");
        AssertUtil.isNull(userStaffDto.getRealName(), "员工真实姓名不能为空");
        //AssertUtil.isNull(userStaffDto.getHospitalDepartId(), "员工科室ID不能为空");
        //AssertUtil.isNull(userStaffDto.getHospitalBaseId(), "员工医院ID不能为空");
        AssertUtil.isNull(userStaffDto.getIdNumber(), "员工身份证号不能为空");
        AssertUtil.isNull(userStaffDto.getJobNumber(), "员工工号不能为空");
        AssertUtil.isNull(userStaffDto.getMobile(), "员工电话不能为空");
        AssertUtil.isNull(userStaffDto.getEmail(), "员工邮箱不能为空");
        TokenInfoBo tokenInfo = LoginAspect.getTokenInfo();
        if (!StringUtil2.isPhone(userStaffDto.getMobile())) {
            throw new EpiCloudException("手机号格式不正确！");
        }
        if (StringUtil.isNullOrEmpty(userStaffDto.getId())) {
            // 新增 先新增sysUser todo 校验手机号是否存在
            SysUser user = new SysUser(userStaffDto);
            sysUserService.checkMobileExist(userStaffDto.getMobile());
            sysUserService.insert(user);
            userStaffDto.setId(StringUtil.get32UUID());
            userStaffDto.setSysUserId(user.getId());
            userStaffDto.setCreateTime(new Date());
            userStaffDto.setModifyTime(new Date());
            userStaffDto.setCreateUser(tokenInfo.getLoginName());
            SysUserStaff userStaff = ReflectUtil.copyProperties(userStaffDto, SysUserStaff.class);
            sysUserStaffMapper.insert(userStaff);
            return userStaff.getId();
        }
        // 编辑
        userStaffDto.setModifyTime(new Date());
        userStaffDto.setModifyUser(tokenInfo.getLoginName());
        SysUserStaff userStaff = ReflectUtil.copyProperties(userStaffDto, SysUserStaff.class);
        sysUserStaffMapper.updateByPrimaryKeySelective(userStaff);
        return userStaff.getId();
    }

    @Override
    public List<SysUserStaffVo> getList(SysUserStaffDto sysUserStaffDto) {
        List<SysUserStaffVo> result;
        Map<String, Object> paramMap = getUserStaffParam(sysUserStaffDto);
        if (sysUserStaffDto.getIsPage()) {
            result = sysUserStaffMapper.getInfoPageByCondition(paramMap);
        } else {
            result = sysUserStaffMapper.getInfoListByCondition(paramMap);
        }
        return result;
    }

    @Override
    public Integer getCount(SysUserStaffDto sysUserStaffDto) {
        Map<String, Object> paramMap = getUserStaffParam(sysUserStaffDto);
        return sysUserStaffMapper.getCountByCondition(paramMap);
    }

    private Map<String, Object> getUserStaffParam(SysUserStaffDto sysUserStaffDto) {
        return ParamsMapUtil.getInstance()
                .addParam("realName", sysUserStaffDto.getRealName())
                .addParam("roleId", sysUserStaffDto.getRoleId())
                .addParam("hospitalDepartId", sysUserStaffDto.getHospitalDepartId())
                .addParam("pageIndex", sysUserStaffDto.getPageIndex())
                .addParam("pageSize", sysUserStaffDto.getPageSize());
    }

    @Override
    public void generateQrCode(String id, HttpServletResponse response) {
        SysUserStaff userStaff = sysUserStaffMapper.selectByPrimaryKey(id);
        String encryptData = EncryptDataUtil.encryptData(CommonConstants.QRCODE_USER_STAFF, userStaff.getSysUserId());
        CodeUtil.encodeQrcode(userStaff.getRealName(), encryptData, response);
    }

    @Override
    public String getIdByUserId(String userId) {
        return sysUserStaffMapper.getIdByUserId(userId);
    }
}